/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.utils;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Tony
 *
 */
public class InteractiveReader extends InputStream {

	private String line;
	private InputStream iStream;
	private int position;
	/**
	 * @param line
	 * @param iStream
	 */
	public InteractiveReader(String line, InputStream iStream) {
		this.line = line;
		this.iStream = iStream;
		this.position = 0;
	}

	/* (non-Javadoc)
	 * @see java.io.Reader#close()
	 */
	@Override
	public void close() throws IOException {
	}

	/* (non-Javadoc)
	 * @see java.io.InputStream#read()
	 */
	@Override
	public int read() throws IOException {
		int result = -1;
		if ( !"".equals(this.line) && this.position >= this.line.length()
			&& !this.line.endsWith(";")) {
			java.util.Scanner scanner = new java.util.Scanner(System.in);
			System.out.print("> ");
			this.line = scanner.nextLine();
			this.position = 0;
		}

		if ( this.line != null && !"".equals(this.line)
			&& this.position < this.line.length()) {
			result = this.line.charAt(this.position++);
		}

		return result;
	}

}
